package cn.iocoder.yudao.module.member.service.userrefund;

import cn.iocoder.yudao.module.infra.util.file.FileUploadUtils;
import cn.iocoder.yudao.module.infra.util.file.vo.FileUploadVo;
import cn.iocoder.yudao.module.member.controller.admin.userpromotionrecords.vo.UserPromotionRecordsRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.dataobject.userwithdraw.UserWithdrawDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import cn.iocoder.yudao.module.member.service.usercommission.UserCommissionService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.module.member.controller.admin.userrefund.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userrefund.UserRefundDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.userrefund.UserRefundMapper;
import org.springframework.web.multipart.MultipartFile;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 退款 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserRefundServiceImpl implements UserRefundService {

    @Resource
    private UserRefundMapper userRefundMapper;
    @Resource
    private MemberUserService memberUserService;
    @Resource
    private ApplicationContext applicationContext;
    @Resource
    private FileUploadUtils fileUploadUtils;

    private UserCommissionService getUserCommissionService() {
        return applicationContext.getBean(UserCommissionService.class);
    }

    @Override
    public Long createUserRefund(UserRefundSaveReqVO createReqVO) {
        // 插入
        UserRefundDO userRefund = BeanUtils.toBean(createReqVO, UserRefundDO.class);
        userRefundMapper.insert(userRefund);
        // 返回
        return userRefund.getId();
    }

    @Override
    public Boolean updateUserRefund(UserRefundUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserRefundExists(updateReqVO.getId());
//        // 更新
//        UserRefundDO updateObj = BeanUtils.toBean(updateReqVO, UserRefundDO.class);
//        userRefundMapper.updateById(updateObj);
        LambdaUpdateWrapper<UserRefundDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(UserRefundDO::getStatus, updateReqVO.getStatus())
                .set(StringUtils.isNoneEmpty(updateReqVO.getScreenshotUrl()), UserRefundDO::getScreenshotUrl, updateReqVO.getScreenshotUrl())
                .set(Objects.nonNull(updateReqVO.getType()), UserRefundDO::getType, updateReqVO.getType())
                .set(StringUtils.isNoneEmpty(updateReqVO.getContact()), UserRefundDO::getContact, updateReqVO.getContact())
                .set(StringUtils.isNoneEmpty(updateReqVO.getRemark()), UserRefundDO::getRemark, updateReqVO.getRemark())
                .eq(UserRefundDO::getId, updateReqVO.getId());
        return userRefundMapper.update(lambdaUpdateWrapper) > 0;
    }

    @Override
    public Boolean updateUserRefund(Long id, MultipartFile[] files, Integer status, Integer type, String contact, String remark) {
        // 校验存在
        validateUserRefundExists(id);
        LambdaUpdateWrapper<UserRefundDO> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        if (Objects.nonNull(files) && (files.length > 0)) {
            FileUploadVo fileUploadVo = fileUploadUtils.upload(files, 1, 10 * 1024 * 1024L);
            lambdaUpdateWrapper.set(UserRefundDO::getScreenshotUrl, fileUploadVo.fileUrls.get(0));
        }
        lambdaUpdateWrapper.set(UserRefundDO::getStatus, status)
                .set(Objects.nonNull(type), UserRefundDO::getType, type)
                .set(StringUtils.isNoneEmpty(contact), UserRefundDO::getContact, contact)
                .set(StringUtils.isNoneEmpty(remark), UserRefundDO::getRemark, remark)
                .eq(UserRefundDO::getId, id);
        return userRefundMapper.update(lambdaUpdateWrapper) > 0;
    }

    @Override
    public void deleteUserRefund(Long id) {
        // 校验存在
        validateUserRefundExists(id);
        // 删除
        userRefundMapper.deleteById(id);
    }

    private void validateUserRefundExists(Long id) {
        if (userRefundMapper.selectById(id) == null) {
            throw exception(USER_REFUND_NOT_EXISTS);
        }
    }

    @Override
    public UserRefundDO getUserRefund(Long id) {
        return userRefundMapper.selectById(id);
    }

    @Override
    public PageResult<UserRefundRespVO> getUserRefundPage(UserRefundPageReqVO pageReqVO) {
        PageResult<UserRefundDO> pageResult = userRefundMapper.selectPage(pageReqVO);
        PageResult<UserRefundRespVO> userRefundRespVOPageResult = BeanUtils.toBean(pageResult, UserRefundRespVO.class);
        userRefundRespVOPageResult.getList().forEach(userRefundRespVO -> {
            MemberUserDO memberUserDO = memberUserService.getUser(userRefundRespVO.getUserId());
            if (Objects.nonNull(memberUserDO)) {
                userRefundRespVO.setNickname(memberUserDO.getNickname());
            }
            userRefundRespVO.setScreenshotUrl(fileUploadUtils.imagePrefixUrl + userRefundRespVO.getScreenshotUrl());
        });
        return userRefundRespVOPageResult;
    }

    @Override
    public Integer totalRefund(Long userId) {
        Integer totalRefund = userRefundMapper.totalRefund(userId);
        return Objects.isNull(totalRefund) ? 0 : totalRefund;
    }

}